var iHeight = 0;   
var iTop = 0;   
var clientHeight = 0; 
var pageNo = 1; 
var pagesize = 10;
var iIntervalId = null;   
  
getPageHeight();   
 
// 添加定时检测事件，每2秒检测一次   
iIntervalId = setInterval("_onScroll();",2000);   
 
// 取得当前页面显示所占用的高度   
function getPageHeight() {  

  if(document.body.clientHeight && document.documentElement.clientHeight) {     
    clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;             
  } else {     
    clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;         
  }   
  iHeight = Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);  

}   
  
// 调用ajax取服务端数据   
function show() {
  $.ajax({   
    url: '?wid='+$("#wid").val()+'&token='+ $("#token").val(),   
    type: 'POST',
    data: {'p': pageNo},   
    dataType: 'text',   
    timeout: 4000,   
    beforeSend: showLoadingImg,   
    error: showFailure,   
    success: showResponse   
  }); 
}   
  
function showLoadingImg() {   
  if(pageNo < 10 ) {   
    $('.more-loading').show();
  } else {   
    clearInterval(iIntervalId);   
    $('.more-loading').hide();
  }   
}   
  
function showFailure() {   
  $('.more-loading').hide();
}   
  
// 根据ajax取出来的json数据转换成html   
function showResponse(responseData) {	

  var returnjson = eval("("+responseData+")");
  var itemsSize = returnjson.length;
  var nextpagehtml = ''; 
  if(itemsSize > 0){
    for(i=0; i<itemsSize; i++) {
      nextpagehtml += "<a href='"+returnjson[i].detail_url+"'>";
      nextpagehtml += "<img src='"+ returnjson[i].pic_url + "'class='img'";
      nextpagehtml += "onerror='this.src=\""+$("#pic_path").val() +"\"'>";
      nextpagehtml += '<h3>'+ returnjson[i].b_name +'</h3>';
      nextpagehtml += '<div class="price"><span class="price-c" style="font-size: 13px;">人均消费:'+ returnjson[i].avg_price +'</span><span class="distance"></span></div>';
      nextpagehtml += '<div class="count"><span class="num">'+ returnjson[i].commend_num +' 赞</span><span class="place"></span></div>';
      nextpagehtml += '</a>';
                             
    }
  }
  if(itemsSize < pagesize){
    $('.more-loading').hide();
    pageNo = 999;
  }  
  $('.deallist').append(nextpagehtml);
    $('.more-loading').hide();
    pageNo++;

}
  
// 判断滚动条是否到达底部   
function reachBottom() {   
  var scrollTop = 0;   
  if(document.documentElement && document.documentElement.scrollTop) {     
    scrollTop = document.documentElement.scrollTop;     
  } else if (document.body) {     
    scrollTop = document.body.scrollTop;     
  }   
  if((scrollTop > 0) && (scrollTop + clientHeight == iHeight)) {   
    return true;     
  } else {     
    return false;    
  }   
}   
  
// 检测事件，检测滚动条是否接近或到达页面的底部区域，0.99是为了更接近底部时   
function _onScroll() {   
  iTop = document.documentElement.scrollTop + document.body.scrollTop;   
  getPageHeight();   
  if(((iTop+clientHeight)>parseInt(iHeight*0.94))||reachBottom()) {   
    if(pageNo > 10) {   
      clearInterval(iIntervalId);   
      $('.more-loading').hide(); 
      return;
    }
    show();   
  }   
};   